All files / src/components/admin BulkImportTVChannelsDialog.tsx

0% Statements 0/68
0% Branches 0/38
0% Functions 0/25
0% Lines 0/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                                                                                                                                                                                                                                                                                                                                                                                                                             
'use client';
 
import { useRef, useState, type ChangeEvent } from 'react';
import { useMutation } from '@tanstack/react-query';
import { z } from 'zod';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { AlertCircle, Upload } from 'lucide-react';
import { contentService } from '@/services';
import { useTranslation } from 'react-i18next';
import useLoadNamespace from '@/hooks/useLoadNamespace';
import { extractErrorMessage } from '@/lib/error-message';
 
type FormData = {
  text: string;
  provider?: string;
  description?: string;
  format?: 'DRM' | 'M3U8';
  country?: string;
  start_channel_number?: number;
};
 
interface Props {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onSuccess: () => void;
}
 
export default function BulkImportTVChannelsDialog({ open, onOpenChange, onSuccess }: Props) {
  useLoadNamespace('admin/bulkImportTVChannels');
  const { t } = useTranslation(['admin/bulkImportTVChannels', 'admin', 'translation']);
  const [form, setForm] = useState<FormData>({ text: '', provider: '', description: '', format: 'DRM', country: '', start_channel_number: undefined });
  const [error, setError] = useState<string | null>(null);
  const fileInputRef = useRef<HTMLInputElement | null>(null);
  // Store result if you need to show a summary in the future
 
  const schema = z.object({
    text: z.string().min(1, t('bulkImportTVChannels.validation.pasteM3uContent')),
    provider: z.string().optional(),
    description: z.string().optional(),
    format: z.enum(['DRM', 'M3U8']).optional(),
    country: z.string().optional(),
    start_channel_number: z
      .string()
      .optional()
      .transform((v) => (v && v.trim() !== '' ? Number(v) : undefined))
      .refine((v) => v === undefined || (Number.isInteger(v) && v > 0), t('bulkImportTVChannels.validation.mustBePositiveNumber'))
  });
 
  const mutation = useMutation({
    mutationFn: async () => {
      setError(null);
      const parsed = schema.safeParse(form);
      if (!parsed.success) {
        const first = (parsed.error as z.ZodError<FormData>)?.issues?.[0]?.message;
        throw new Error(first || t('bulkImportTVChannels.validation.invalidData'));
      }
      const payload = {
        text: parsed.data.text,
        provider: parsed.data.provider || undefined,
        description: parsed.data.description || undefined,
        format: parsed.data.format || undefined,
        country: parsed.data.country || undefined,
        start_channel_number: parsed.data.start_channel_number};
      const res = await contentService.bulkImportTVChannels(payload);
      if (!res.success) {
        throw new Error(extractErrorMessage(res.error, t('bulkImportTVChannels.validation.bulkImportFailed')));
      }
      return res.data || [];
    },
    onSuccess: () => {
      onSuccess();
      onOpenChange(false);
      setForm({ text: '', provider: form.provider, description: '', format: form.format, country: form.country, start_channel_number: undefined });
    },
    onError: (e: unknown) => {
      const msg = e instanceof Error ? e.message : t('bulkImportTVChannels.validation.unknownError');
      setError(msg);
    }});
 
  const handleChooseFile = () => {
    fileInputRef.current?.click();
  };
 
  const handleFileChange = async (e: ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    if (!file) return;
 
    try {
      const fileText = await file.text();
      setError(null);
      setForm((f) => ({ ...f, text: fileText }));
    } catch {
      setError(t('bulkImportTVChannels.validation.fileReadFailed'));
    } finally {
      e.target.value = '';
    }
  };
 
  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-2xl max-h-[85vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle>{t('bulkImportTVChannels.title')}</DialogTitle>
          <DialogDescription>{t('bulkImportTVChannels.description')}</DialogDescription>
        </DialogHeader>
 
        {error && (
          <Alert variant="destructive" className="mb-2">
            <AlertCircle className="h-4 w-4" />
            <AlertDescription>{error}</AlertDescription>
          </Alert>
        )}
 
        <div className="grid gap-4 py-2">
          <div className="grid gap-2">
            <Label>{t('bulkImportTVChannels.labels.format')}</Label>
            <div className="flex gap-3">
              <label className="flex items-center gap-2 text-sm">
                <input
                  type="radio"
                  name="format"
                  checked={form.format === 'DRM'}
                  onChange={() => setForm((f) => ({ ...f, format: 'DRM' }))}
                />
                DRM
              </label>
              <label className="flex items-center gap-2 text-sm">
                <input
                  type="radio"
                  name="format"
                  checked={form.format === 'M3U8'}
                  onChange={() => setForm((f) => ({ ...f, format: 'M3U8' }))}
                />
                M3U8
              </label>
            </div>
          </div>
 
          <div className="grid gap-2">
            <Label>{t('bulkImportTVChannels.labels.provider')}</Label>
            <Input value={form.provider || ''} onChange={(e) => setForm((f) => ({ ...f, provider: e.target.value }))} placeholder={t('bulkImportTVChannels.placeholders.provider')} />
          </div>
 
            <div className="grid gap-2">
              <Label>{t('bulkImportTVChannels.labels.globalDescription')}</Label>
              <Input value={form.description || ''} onChange={(e) => setForm((f) => ({ ...f, description: e.target.value }))} placeholder={t('bulkImportTVChannels.placeholders.description')} />
            </div>
 
          <div className="grid gap-2">
            <Label>{t('bulkImportTVChannels.labels.country')}</Label>
            <Input value={form.country || ''} onChange={(e) => setForm((f) => ({ ...f, country: e.target.value }))} placeholder={t('bulkImportTVChannels.placeholders.country')} />
          </div>
 
          <div className="grid gap-2">
            <Label>{t('bulkImportTVChannels.labels.startChannelNumber')}</Label>
            <Input
              type="number"
              min={1}
              value={form.start_channel_number ? String(form.start_channel_number) : ''}
              onChange={(e) => setForm((f) => ({ ...f, start_channel_number: e.target.value ? Number(e.target.value) : undefined }))}
              placeholder={t('bulkImportTVChannels.placeholders.startChannelNumber')}
            />
          </div>
 
          <div className="grid gap-2">
            <div className="flex items-center justify-between gap-3">
              <Label>{t('bulkImportTVChannels.labels.m3uList')}</Label>
              <div className="flex items-center gap-2">
                <input
                  ref={fileInputRef}
                  type="file"
                  accept=".m3u,.m3u8,text/plain"
                  className="hidden"
                  onChange={handleFileChange}
                />
                <Button type="button" size="sm" variant="outline" onClick={handleChooseFile}>
                  {t('bulkImportTVChannels.buttons.chooseFile')}
                </Button>
              </div>
            </div>
            <Textarea
              rows={12}
              value={form.text}
              onChange={(e) => setForm((f) => ({ ...f, text: e.target.value }))}
              className="h-64 max-h-[60vh] overflow-y-auto resize-y"
              placeholder={`#EXTM3U\n\n#EXTINF:-1 tvg-name="STARZ COMEDY" tvg-logo="https://...",STARZ COMEDY\n#KODIPROP:inputstream.adaptive.license_type=clearkey\n#KODIPROP:inputstream.adaptive.license_key=KID:KEY o {"kid":"key"}\nhttps://.../cenc.mpd`}
            />
          </div>
        </div>
 
        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)} disabled={mutation.isPending}>{t('bulkImportTVChannels.buttons.cancel')}</Button>
          <Button onClick={() => mutation.mutate()} disabled={mutation.isPending}>
            <Upload className="h-4 w-4 mr-2" />
            {t('bulkImportTVChannels.buttons.import')}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}